package com.h3bpm.web.instance.consultingOutcomeDocument.dao.Impl;

import com.alibaba.fastjson.JSONObject;
import com.h3bpm.handler.JSONUtils;
import com.h3bpm.web.common.utils.SqlUtil;
import com.h3bpm.web.config.UrlConfig;
import com.h3bpm.web.instance.consultingOutcomeDocument.ConsultingOutController;
import com.h3bpm.web.instance.consultingOutcomeDocument.dao.ReAttachteem;
import com.h3bpm.web.protalHanding.OrcaleHelper;
import com.h3bpm.web.protalHanding.ThreadPost;
import com.h3bpm.web.util.DateUtil;
import data.DataRow;
import data.DataTable;
import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Repository;

import java.util.*;

/**
 * @author villwang
 * @date 2020/07/23
 **/
@Repository
public class ReAttachteemImpl implements ReAttachteem {
    private static final Logger logger = LoggerFactory.getLogger(ReAttachteemImpl.class);

    @Override
    public String reAttachteem(String objectId,String businessId) {
        String BusinessUrl= UrlConfig.BusinessUrl;
        try {
        String sql="SELECT dataField,fileName,contentType,contentLength,storagePath FROM ot_attachment WHERE " +
                "BizObjectId='"+objectId+"'";
        DataTable data = null;
        data = SqlUtil.doQuery(sql);
        HashMap<String, Object> maps = new HashMap<>();
        ArrayList<HashMap<String, Object>> bmsAttachments = new ArrayList<>();
        List<DataRow> entityRows = data.getEntityRows();
        if(!entityRows.isEmpty()&&entityRows.size()>0) {
            for (DataRow userData : entityRows) {
                HashMap<String, Object> map = new HashMap<>();
                String dataField=userData.getString("dataField");
                String fileName=userData.getString("fileName");
                String contentType=userData.getString("contentType");
                String contentLength=userData.getString("contentLength");
                String storagePath=userData.getString("storagePath");
                map.put("dataField",dataField);
                map.put("category","05");
                map.put("fileName",fileName);
                map.put("contentType",contentType);
                map.put("contentLength",contentLength);
                map.put("storagePath","/"+storagePath+"/"+fileName);
                bmsAttachments.add(map);
            }
        }
            maps.put("bmsAttachments",bmsAttachments);
            maps.put("bizId",businessId);
            String url =BusinessUrl+"/prod-api/workflow/createAttachments";
            String json = JSONUtils.toJson(maps);
            Map<String, Object> result = ThreadPost.postRequestResult(url,json,null);
            if(result==null||result.size()==0){
                logger.debug("返回值为空—————————————");
            }else {
                int res= (Integer) result.get("code");
                if(res==200){
                    logger.debug("推送附件成功====================success"+result.get("msg"));

                }else {
                    logger.debug("推送附件失败====================失败");
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

    @Override
    public String getDirectory(String name,String businessId,String objectId) {
        String BusinessUrl= UrlConfig.BusinessUrl;

        HashMap<String, Object> maps = new HashMap<>();
        ArrayList<HashMap<String, Object>> bmsCatalogVos = new ArrayList<>();
        HashMap<String, Object> map = new HashMap<>();
        map.put("catalogName",name);
        map.put("type","1");
        HashMap<String, Object> map2 = new HashMap<>();
        map2.put("catalogName",getTime(objectId));
        //子
        map2.put("type","0");
        bmsCatalogVos.add(map);
        bmsCatalogVos.add(map2);
        maps.put("bmsCatalogVos",bmsCatalogVos);
        maps.put("businessId",businessId);
        String url =BusinessUrl+"/prod-api/workflow/createCatalog";
        String json = JSONUtils.toJson(maps);
        Map<String, Object> result = ThreadPost.postRequestResult(url,json,null);
        if(result==null||result.size()==0){
            logger.debug("返回值为空—————————————");
        }else {
            int res= (Integer) result.get("code");
            if(res==200){
                logger.debug("创建目录====================success"+result.get("data"));
                return (String) result.get("data");
            }else {
                logger.debug("创建目录====================失败");
            }
        }
        return null;
    }
    @Override
    public String getTime(String objectId) {
        if(StringUtils.isNotBlank(objectId)){
            try {
                String sql="SELECT createdTime FROM ot_instancecontext WHERE BizObjectId='" +objectId+"'";
                DataTable table = SqlUtil.doQuery(sql);
                List<DataRow> entityRows = table.getEntityRows();
                if(!entityRows.isEmpty()&&entityRows.size()>0) {
                    String createdTime= entityRows.get(0).getString("createdTime");
                    logger.info("获取发起时间————createdTime---"+createdTime);
                    String[] strings = createdTime.split(" ");
                    String cc=strings[0].replaceAll("-","");
                    return cc;
                }

            }catch (Exception e){
                e.printStackTrace();
            }
        }
        return DateUtil.formatDate4(new Date());
    }

}
